package com.daylightrobbery.service.impl;

import com.daylightrobbery.entity.Account;
import com.daylightrobbery.mapper.AccountMapper;
import com.daylightrobbery.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    public int addAccount(Account account) {
        return accountMapper.insertAccount(account);
    }

    public Account getAccount(String uid) {
        return accountMapper.findAccountById(uid);
    }

    @Override
    public Account getAccountByAccount(String account) {
        return accountMapper.findAccountByAccount(account);
    }

    public void updateAccount(Account account) {
        accountMapper.updateAccount(account);
    }

    public void deleteAccount(int uid) {
        accountMapper.deleteAccount(uid);
    }

    @Override
    public Account checkUserAndGetInfo(Account account) {
        if (account == null) {
            return null;
        }
        Account accountInDB = accountMapper.findAccountByAccount(account.getAccount());
        if (accountInDB == null)
            return null;
        if(accountInDB.getAccount() != null && accountInDB.getAccount().equals(account.getAccount())
                && accountInDB.getAccount() != null
                && accountInDB.getPassword().equals(account.getPassword())
        ){
            return accountInDB.setPassword(null);
        }
        return null;
    }

}
